package client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
// unused
import java.util.Scanner;
import java.util.*;


public class Client_login {
	public boolean done = false;
	public Client_login() {
		super();
	}
	
	public void login() {
	    byte[] byteArray;
	    File reference = null;
	    String IP = "127.0.0.1";
	    String answer;
	    while(!done) {	    
	    try {
	    	Socket socket = new Socket(IP, 8888);
	        if(socket.isConnected()) {
	        	System.out.println("Connexion established with the Server");
	        	DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
	        	DataInputStream dIn = new DataInputStream(socket.getInputStream());

	        	Scanner sc = new Scanner(System.in);
	        	
		        	System.out.println("ask for ? 1 : creating account / 2 : login / 3 quit :");
		        	String str = sc.nextLine();
			        dOut.writeByte(Integer.parseInt(str));
			        
			        if(Integer.parseInt(str) == 1) {
			        	System.out.println("Enter your login_name : ");
				        str = sc.nextLine();
				        dOut.writeUTF(str);
				        System.out.println("Enter voice record official name to send : ");
				        str = sc.nextLine();
			        	sendFile(str, dIn, dOut);
			        	
			        	// Ack
			        	answer = dIn.readUTF();
			        	if(Integer.parseInt(answer) == 1) {
			        		System.out.println("Add account OK");
			        	} else {
			        		System.out.println("Add account ERROR");
			        	}
			        	
			        	dOut.writeByte(3);
				        dOut.flush();
			        } else if(Integer.parseInt(str) == 2) {
			        	System.out.println("Enter your login_name : ");
				        str = sc.nextLine();
				        dOut.writeUTF(str);
				        System.out.println("Enter voice record name to send : ");
				        str = sc.nextLine();
			        	sendFile(str, dIn, dOut);
			        	
			        	// Ack
			        	answer = dIn.readUTF();
			        	if(Integer.parseInt(answer) == 1) {
			        		System.out.println("Login win");
			        	} else {
			        		System.out.println("Login fail or account not found !");
			        	}
			        	
			        	dOut.writeByte(3);
				        dOut.flush();
			        } else {
			        	done = true;
			        	dOut.writeByte(3);
			        	dOut.flush();
			        }
	        	}
		        ////////////////////////////////////////////////////////
		       /* sendFile("bonjour.wav", dIn, dOut);
		        dOut.writeByte(3);
		        dOut.flush();*/
				////////////////////////////////////////////////////////

		        socket.close();
	        
	    } catch (Exception e) {
	    	System.out.println("[ERROR] : "+e);
        }
	    }
	}  
	
	static public void sendInChunks(DataInputStream input, DataOutputStream output, int togo, int chunksize) throws IOException {
		byte bytes[] = new byte[chunksize];
		int writecount;
		while (togo > 0) {
		    writecount = (togo >= chunksize ? chunksize : togo);
		    input.read(bytes, 0, writecount);
		    output.write(bytes, 0,  writecount);
		    togo = togo - writecount;
		}
		
		output.flush();
    }
    

    static private void sendFile(String send, DataInputStream input, DataOutputStream output) {

		String ack=null;              
		System.out.println("Preparing to transfer file " + send + "...");
	
		try {
		    output.writeUTF(send);    
		    ack = input.readUTF();    
		    if (! ack.equals("ACK")) {
		    	System.out.println("Server didn't acknowledge send.  Aborted.");
		    } else {
				File file = new File(send);
				DataInputStream filestream=new DataInputStream(new FileInputStream(send));
				int togo = filestream.available();
		
				// push length of file to server
				output.writeInt(togo);
		
				// transfer file contents
				sendInChunks(filestream, output, togo, 1024);
				filestream.close();
		
				// is the server happy with the transfer?
				ack = input.readUTF();
				if (! ack.equals("ACK")) {
				    System.out.println("Server failed to store the file.");
				} else {
				    System.out.println("Store successful.");
				}
		    } 
		} catch (Exception e) {
		    System.out.println("Server failed to store the file.");
		}
    }
}
